#pragma once

#include <jsoncpp/json/json.h>
#include <iostream>
#include <string>

using namespace std;


typedef struct request
{
    int _dataFirst;
    int _dataSecond;
    char _op;
}request_t;

typedef struct response
{
    int code = 0;// 状态码，0为正常，1为除0错误，2为模0错误，3为其他错误
    int result;
}response_t;

string SerializeRequest(const request_t& rq)
{
    Json::FastWriter writer;
    Json::Value root;

    root["numFirst"] = rq._dataFirst;
    root["numSecond"] = rq._dataSecond;
    root["operator"] = rq._op;

    return writer.write(root);
}

void DeserializeRequest(const string& json_str, request_t& out)
{
    Json::Reader reader;
    Json::Value root;
    reader.parse(json_str, root);
    out._dataFirst = root["numFirst"].asInt();
    out._dataSecond = root["numSecond"].asInt();
    out._op = root["operator"].asUInt();
}

string SerializeResponse(const response_t& rp)
{
    Json::FastWriter writer;
    Json::Value root;

    root["code"] = rp.code;
    root["result"] = rp.result;

    return writer.write(root);
}

void DeserializeResponse(const string& json_str, response_t& out)
{
    Json::Reader reader;
    Json::Value root;

    reader.parse(json_str, root);
    out.code = root["code"].asInt();
    out.result = root["result"].asInt();
}